package alibaba.testutils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import alibaba.AbstractAlibabaTest;
import alibaba.common.utils.CommonUtils;

public class FileTestUtils {

	public static final long KB = 1024;
	public static final long MB = 1024*KB;
	
	public static void printTextFileContent(String path) throws IOException {
		BufferedReader reader = new BufferedReader(new FileReader(path));
		String line;
		while ((line = reader.readLine()) != null) {
			System.out.println(line);
		}
		reader.close();
	}
	
	public static File[] getFilesOnServer() {
		File file = new File(AbstractAlibabaTest.SERVER_BACKUP_DIR);
		return file.listFiles();
	}

	public static void createFile(String path, String content)
			throws IOException {
		FileWriter writer = new FileWriter(path);
		StringReader reader = new StringReader(content);
		CommonUtils.readReaderIntoWriter(reader, writer);
		writer.close();
	}

	public static long createFile(String path, long size) throws IOException {
		File file = new File(path);
		FileWriter writer = new FileWriter(path);
		StringReader reader = new StringReader(
				"This is going to be a big text file\n");
		while (file.length() < size) {
			CommonUtils.readReaderIntoWriter(reader, writer);
			reader = new StringReader(
					"Text text text text text text text text text text\n");
		}
		writer.close();
		return file.length();
	}

	public static void appendToFile(String path, String content)
			throws IOException {
		FileWriter writer = new FileWriter(path, true);
		StringReader reader = new StringReader(content);
		CommonUtils.readReaderIntoWriter(reader, writer);
		writer.close();
	}

	public static void createDirectory(String dirPath) {
		File file = new File(dirPath);
		if (!file.exists()) {
			file.mkdirs();
		}
	}

	public static void deleteDirectory(String dirPath) {
		File path = new File(dirPath);
		if (path.exists()) {
			File[] files = path.listFiles();
			for (int i = 0; i < files.length; i++) {
				if (files[i].isDirectory()) {
					deleteDirectory(files[i].getAbsolutePath());
				} else {
					files[i].delete();
				}
			}
		}
		path.delete();
	}

	public static List<String> compareFiles(File file1, File file2) throws IOException
	{
		List<String> diffs = new ArrayList<String>();	
		Reader r1 = new FileReader(file1);
		Reader r2 = new FileReader(file2);
		
		LineNumberReader reader1 = new LineNumberReader(r1);
		LineNumberReader reader2 = new LineNumberReader(r2);
		String line1 = reader1.readLine();
		String line2 = reader2.readLine();
		while (line1 != null && line2 != null) {
			if (!line1.equals(line2)) {
				diffs.add("File \"" + file1.getName() + "\" and file \"" + file2.getName() + "\" differ at line " + 
						reader1.getLineNumber() + ":" + "\n" + line1 + "\n" + line2);
				break;
			}
			line1 = reader1.readLine();
			line2 = reader2.readLine();
		}
		if (line1 == null && line2 != null)
			diffs.add("File \"" + file2.getName() + "\" has extra lines at line " + reader2.getLineNumber() + ":\n" + line2);
		if (line1 != null && line2 == null)
			diffs.add("File \"" + file1.getName() + "\" has extra lines at line " + reader1.getLineNumber() + ":\n" + line1);
		return diffs;
	}
}
